package cn.core.coding.rest;

import cn.core.coding.ao.ProConfAndDbTableConfAO;
import cn.core.coding.ao.ProConfAO;
import cn.core.coding.service.intf.ICodingService;
import cn.core.coding.vo.DbTableConfAndInfoVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.util.CoreTool;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.hikari.HikariCpConfig;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 直接生成代码 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("coding/to")
public class ToCodingController {

  @Resource
  private ICodingService codingService;

  @Resource
  private DataSource dataSource;

  /**
   * 添加数据源
   * http://127.0.0.1:6663/api/coding/to/addDataSource
   * @return 返回
   */
  @GetMapping("addDataSource")
  public String addDataSource() throws Exception {
    try {
      String dbType = "mysql";
      String ipAddress = "127.0.0.1";
      String port = "3306";
      String username = "root";
      String password = "Moonnow166!";

      String dbName = "pro-coding";

//      String dbType = "oracle";
//      String ipAddress = "127.0.0.1";
//      String port = "9521";
//      String username = "pro";
//      String password = "pro";

      String serverName = "ORCLPDB";

      String poolName = "moonnowds";
      String driverClassName = null;
      String urlStr = null;
      if (CoreTool.mysql.equals(dbType)) {
        driverClassName = "com.mysql.cj.jdbc.Driver";
        urlStr = "jdbc:mysql://" + ipAddress + ":" + port + "/" + dbName + "?useUnicode=yes&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&allowMultiQueries=true&rewriteBatchedStatements=true";
      } else if (CoreTool.oracle.equals(dbType)) {
        driverClassName = "oracle.jdbc.driver.OracleDriver";
        urlStr = "jdbc:oracle:thin:@" + ipAddress + ":" + port + "/" + serverName;
      } else {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
      if (ds.getCurrentDataSources().containsKey(poolName)) {
        throw new CoreException("数据源已经加载，请不要重复加载！");
      }
      DataSourceProperty dataSourceProperty = new DataSourceProperty();
      dataSourceProperty.setPoolName(poolName);
      dataSourceProperty.setDriverClassName(driverClassName);
      dataSourceProperty.setUrl(urlStr);
      dataSourceProperty.setUsername(username);
      dataSourceProperty.setPassword(password);
      dataSourceProperty.setLazy(false);
      HikariCpConfig hikariCpConfig = new HikariCpConfig();
      hikariCpConfig.setMinIdle(1);
      hikariCpConfig.setMaxPoolSize(5);
      DataSource newDataSource = new HikariDataSourceCreator(hikariCpConfig).createDataSource(dataSourceProperty);
      ds.addDataSource(poolName, newDataSource);
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "数据源添加成功";
  }

  /**
   * 生成项目模块
   * http://127.0.0.1:6663/api/coding/to/generateProjectModule
   * @return 返回
   */
  @GetMapping("generateProjectModule")
  public String generateProjectModule() throws Exception {
    try {
      ProConfAO proConfParam = new ProConfAO()
          .setCodeCi("ci_2_space")
          .setProjectPath("/Users/moonnow/wode/t3/pro/gm")
          .setProjectModuleName("sign")
          .setProjectModuleNotes("权限登录模块");
      codingService.generateProjectModule(codingService.getProjectModuleConf(proConfParam));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "项目模块生成成功";
  }

  /**
   * 生成代码 >>> pro
   * http://127.0.0.1:6663/api/coding/to/generate
   * @return 返回
   */
  @GetMapping("generate")
  public String generate() throws Exception {
    try {
      ProConfAndDbTableConfAO proConfAndDbTableConfParam = new ProConfAndDbTableConfAO()
          .setCodeCi("ci_2_space")
          .setProjectPath("/Users/moonnow/wode/t3/pro/gm/coding")
          .setProjectPathVue("/Users/moonnow/wode/t3/pro/gm/coding/vuepro")
          .setProjectModuleName("coding")
          .setDbType("mysql")
          .setDbTablePrefix("COD_")
          .setDsName("moonnowds")
          .setDbName("pro-coding")
          .setTableName("COD_DB_TABLE_CONF");
      DbTableConfAndInfoVO dbTableConfAndInfoVO = codingService.getDbTableConfAndInfo(proConfAndDbTableConfParam);
      codingService.generateEntityFile(dbTableConfAndInfoVO);
      codingService.generateQueryFile(dbTableConfAndInfoVO);
      codingService.generateVoFile(dbTableConfAndInfoVO);
      codingService.generateMapperPersistentInterfacesFile(dbTableConfAndInfoVO);
      codingService.generateMapperSqlFile(dbTableConfAndInfoVO);
      codingService.generateServiceInterfacesFile(dbTableConfAndInfoVO);
      codingService.generateServiceImplementFile(dbTableConfAndInfoVO);
      codingService.generateRestFile(dbTableConfAndInfoVO);
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "代码生成成功";
  }

  /**
   * 删除代码文件 >>> pro
   * http://127.0.0.1:6663/api/coding/to/delProCodeFiles
   * @return 返回
   */
  @GetMapping("delProCodeFiles")
  public String delProCodeFiles() throws Exception {
    try {
      ProConfAndDbTableConfAO proConfAndDbTableConfParam = new ProConfAndDbTableConfAO()
          .setCodeCi("ci_2_space")
          .setProjectPath("/Users/moonnow/wode/cs/2")
          .setProjectModuleName("dict")
          .setDbType("mysql")
          .setDbTablePrefix("SYS_")
          .setDsName("moonnowds")
          .setDbName("hh")
          .setTableName("SYS_DICT_VALUE");
      codingService.delProCodeFiles(codingService.getDbTableConfAndInfo(proConfAndDbTableConfParam));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "pro代码文件删除成功";
  }

  /**
   * 生成代码 >>> vue
   * http://127.0.0.1:6663/api/coding/to/generateVue
   * @return 返回
   */
  @GetMapping("generateVue")
  public String generateVue() throws Exception {
    try {
      ProConfAndDbTableConfAO proConfAndDbTableConfParam = new ProConfAndDbTableConfAO()
          .setCodeCi("ci_2_space")
          .setProjectPath("/Users/moonnow/wode/t3/pro/gm/coding")
          .setProjectPathVue("/Users/moonnow/wode/t3/pro/gm/coding/vuepro")
          .setProjectModuleName("coding")
          .setDbType("mysql")
          .setDbTablePrefix("COD_")
          .setDsName("moonnowds")
          .setDbName("pro-coding")
          .setTableName("COD_DB_TABLE_CONF");
      DbTableConfAndInfoVO dbTableConfAndInfoVO = codingService.getDbTableConfAndInfo(proConfAndDbTableConfParam);
      codingService.generateVueEntityFile(dbTableConfAndInfoVO);
      codingService.generateVueQueryFile(dbTableConfAndInfoVO);
      codingService.generateVueVoFile(dbTableConfAndInfoVO);
      codingService.generateVueApiServiceFile(dbTableConfAndInfoVO);
      codingService.generateVueStoreFile(dbTableConfAndInfoVO);
      codingService.generateVueViewFile(dbTableConfAndInfoVO);
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "vue代码生成成功";
  }

  /**
   * 删除代码文件 >>> vue
   * http://127.0.0.1:6663/api/coding/to/delVueCodeFiles
   * @return 返回
   */
  @GetMapping("delVueCodeFiles")
  public String delVueCodeFiles() throws Exception {
    try {
      ProConfAndDbTableConfAO proConfAndDbTableConfParam = new ProConfAndDbTableConfAO()
          .setCodeCi("ci_2_space")
          .setProjectPath("/Users/moonnow/wode/cs/2")
          .setProjectPathVue("/Users/moonnow/wode/cs/2")
          .setProjectModuleName("dict")
          .setDbType("mysql")
          .setDbTablePrefix("SYS_")
          .setDsName("moonnowds")
          .setDbName("hh")
          .setTableName("SYS_DICT");
      codingService.delVueCodeFiles(codingService.getDbTableConfAndInfo(proConfAndDbTableConfParam));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "vue代码文件删除成功";
  }

  @Resource
  private Gson gson;

  /**
   * uuid32
   * http://127.0.0.1:6663/api/coding/to/uuid32
   * @return 返回
   */
  @GetMapping("uuid32")
  public String uuid32() throws Exception {
    try {
      Long a = 1000000000l;
      Long aa = 1000000000l;
      if (a != aa) {
        System.out.println("aaaaaaaaaaaaaaaaaaa");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "";
  }

}
